package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectReportFrequencyService{

	@Autowired
	private ProjectReportFrequencyDao projectReportFrequencyDao;

	@Autowired
	private ProjectDao projectDao;
	
	private QProjectReportFrequency $ = QProjectReportFrequency.projectReportFrequency;
	
	private QProject $_ = QProject.project;
	
	@Autowired
	public void setProjectReportFrequencyDao(ProjectReportFrequencyDao projectReportFrequencyDao) {
		this.projectReportFrequencyDao = projectReportFrequencyDao;
	}
	
	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	public List<ProjectReportFrequency> findAll(){
		return ListUtil.toList(projectReportFrequencyDao.findAll());
	}
				
	public List<ProjectReportFrequency> findAll(
	String code,
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(code)){
		condition.and($.code.contains(code));
	}
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(projectReportFrequencyDao.findAll(condition));
	}		

	public Page<ProjectReportFrequency> findAll(
			String code,
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(code)){
				condition.and($.code.contains(code));
			}
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			Sort sortObject = new Sort(Direction.DESC, "orderNumber");
			*/
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return projectReportFrequencyDao.findAll(condition, pageRequest);
	}
	
	private boolean findProjectByProjectReportFrequency(Long projectReportFrequencyId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.projectReportFrequency.id.eq(projectReportFrequencyId));
		return this.projectDao.count(condition) > 0;
	}
	
	public ProjectReportFrequency getProjectReportFrequency(Long id){
		return projectReportFrequencyDao.getOne(id);
	}
	
	public void deleteProjectReportFrequency(Long id) throws CannotDelException{
		if (this.findProjectByProjectReportFrequency(id)) {
			throw new CannotDelException("项目报告频度已经被项目引用，请先删除项目或修改项目的引用！");
		} else {
			projectReportFrequencyDao.delete(id);
		}
	}

	public void saveProjectReportFrequency(ProjectReportFrequency projectReportFrequency) {
		if (null == projectReportFrequency.getOrderNumber()) {
			projectReportFrequency.setOrderNumber(this.getOrderNumber());
		}
		projectReportFrequencyDao.save(projectReportFrequency);
	}

	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<ProjectReportFrequency> projectReportFrequencys = this.projectReportFrequencyDao.findAll(condition,pageRequest);
		if(projectReportFrequencys.getTotalElements() > 0){
			ProjectReportFrequency projectReportFrequency = projectReportFrequencys.getContent().get(0);
			return projectReportFrequency.getOrderNumber()+1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		ProjectReportFrequency currentModule = this.projectReportFrequencyDao.findOne(id);
		ProjectReportFrequency targetModule = this.projectReportFrequencyDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.projectReportFrequencyDao.save(currentModule);
		this.projectReportFrequencyDao.save(targetModule);
		return true;
	}

	public ProjectReportFrequency findOne(Long id) {
		return this.projectReportFrequencyDao.findOne(id);
	}
	
	

}
		
